package com.java46.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java46.entity.GenericResultEntity;
import com.java46.entity.News;
import com.java46.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName NewsController
 * @Description 新闻控制中心层
 * @Author Tigger Wu
 * @Date 2018/8/1817:13
 * @Version 1.0
 **/
@Controller()
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;
/*
*
 *   分页查询新闻信息
 *
 * @author Tigger Wu
 * @date 2018/8/20 17:33
 * @param [page, request, model]
 * @return java.lang.String
 */
    @RequestMapping(value = "/queryList",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryList(Integer page,HttpServletRequest request, Model model) throws JsonProcessingException {
        //查询全部新闻
        PageInfo<News> nlist = newsService.queryList(page);
        //创建结果实体并赋值
        GenericResultEntity result = new GenericResultEntity();
        result.setSuccess(true);
        result.setData(nlist);
        //参数map
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("news",result);
        //转为json
        ObjectMapper mapper= new ObjectMapper();
        String jsonStr =  mapper.writeValueAsString(map);
        return jsonStr;
    }

    /*
    *
     *  查询最近6条信息
     *
     * @author Tigger Wu
     * @date 2018/8/20 17:33
     * @param
     * @return
     */
    @RequestMapping(value = "/queryLast",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryLast() throws JsonProcessingException {
        //查询最近的六条新闻
        List<News>news = newsService.quertLastNews();
        //创建结果集
        //创建结果实体并赋值
        GenericResultEntity result = new GenericResultEntity();
        result.setSuccess(true);
        result.setData(news);
        //转为json
        ObjectMapper mapper= new ObjectMapper();
        String jsonStr =  mapper.writeValueAsString(result);
        return jsonStr;
    }
    /*
    *
     *  根据ID 查询新闻
     *
     * @author Tigger Wu
     * @date 2018/8/20 18:08
     * @param
     * @return
     */
    @RequestMapping(value = {"/queryById","/single"},produces = "application/json;charset=utf-8")
    public String queryById(Integer id,Model model,HttpServletRequest request) throws JsonProcessingException {
        //根据id查询
        News n = newsService.queryById(id);
        HttpSession session = request.getSession();
        session.setAttribute("news",n);
        return "redirect: /single";
    }
}
